<template>
	<el-dialog :title="form.appId ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="app名字" prop="appName">
						<el-input v-model="form.appName" placeholder="请输入app名字" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应用编码" prop="appCd">
						<el-input v-model="form.appCd" placeholder="请输入应用编码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="api密钥" prop="apiKey">
						<el-input v-model="form.apiKey" placeholder="请输入api密钥" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应用公钥" prop="appPublicSecret">
						<el-input v-model="form.appPublicSecret" placeholder="请输入应用公钥" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="应用私钥" prop="appPrivateSecret">
						<el-input v-model="form.appPrivateSecret" placeholder="请输入应用私钥" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="回调url" prop="notifyUrl">
						<el-input v-model="form.notifyUrl" placeholder="请输入回调url" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="app二维码" prop="appErcode">
						<el-input v-model="form.appErcode" placeholder="请输入app二维码" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="商户ID" prop="merchantId">
						<el-input v-model="form.merchantId" placeholder="请输入商户ID" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="状态" prop="status">
						<el-input v-model="form.status" placeholder="请输入状态" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="创建时间" prop="createAt">
						<el-input v-model="form.createAt" placeholder="请输入创建时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="更新时间" prop="updateAt">
						<el-input v-model="form.updateAt" placeholder="请输入更新时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="租户" prop="tanentId">
						<el-input v-model="form.tanentId" placeholder="请输入租户" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="MerchantAppDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, validateExist } from '/@/api/payment/MerchantApp';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	appId: '',
	appName: '',
	appCd: '',
	apiKey: '',
	appPublicSecret: '',
	appPrivateSecret: '',
	notifyUrl: '',
	appErcode: '',
	merchantId: '',
	status: '',
	createAt: '',
	updateAt: '',
	tanentId: '',
});

// 定义校验规则
const dataRules = ref({});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.appId = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取merchantApp信息
	if (id) {
		form.appId = id;
		getMerchantAppData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.appId ? await putObj(form) : await addObj(form);
		useMessage().success(form.appId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getMerchantAppData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj({ appId: id })
		.then((res: any) => {
			Object.assign(form, res.data[0]);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
